package mail;

import com.chilkatsoft.CkEmail;
import com.chilkatsoft.CkMailMan;

import cube.data.util.DataUtil;
import cube.utils.InputArgs;
import cube.utils.SystemUtil;

/**
 * 邮件发送器（Windows32位版本） 支持通过代理发送
 */
public class MailSender {

	/**
	 * 默认发送邮箱用户名
	 */
	private static final String DEFAULT_MAIL_USERNAME = "xxx";

	/**
	 * 默认发送邮箱密码
	 */
	private static final String DEFAULT_MAIL_PASSWORD = "xxx";

	/**
	 * 默认发送邮箱域名
	 */
	private static final String DEFAULT_MAIL_DOMAIN = "xxx@x.com";

	/**
	 * 注册组件
	 */
	static {
		registMailCompoment();
	}

	/**
	 * 注册邮件发送组件
	 */
	private static void registMailCompoment() {
		try {
			SystemUtil.loadFromJar("chilkat.dll");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Main
	 */
	public static void main(String[] args) {
		InputArgs as = getInputArgs(args);
		CkMailMan mm = getMailSender(as);
		CkEmail mail = getMail(as);
		sendMail(mm, mail);
	}

	/**
	 * 转换输入参数
	 */
	private static InputArgs getInputArgs(String[] args) {
		StringBuffer help = new StringBuffer();
		help.append("usage:\n");
		help
				.append("[-p proxy_ip:proxy_port] -t mailTo -s subject [-b body] [-a attachment]\n");
		help
				.append("[-p proxy_ip:proxy_port] -t mailTo [-s subject] [-b body] -a attachment\n");
		InputArgs as = new InputArgs(args, help.toString());
		return as;
	}

	/**
	 * 初始化邮件发送器
	 */
	private static CkMailMan getMailSender(InputArgs args) {
		CkMailMan sender = new CkMailMan();
		sender.UnlockComponent("ZIP ZIPT34MB34N_0F616ABFp16E");
		setMailProxy(args, sender);
		setMailSenderInfo(sender);
		return sender;
	}

	/**
	 * 设置邮件代理
	 */
	private static void setMailProxy(InputArgs args, CkMailMan mm) {
		try {
			String proxy = args.get("-p");
			if (proxy != null) {
				String[] proxys = proxy.split(":");
				String proxy_ip = proxys[0];
				int proxy_port = DataUtil.toInt(proxys[1]);
				mm.put_HttpProxyHostname(proxy_ip);
				mm.put_HttpProxyPort(proxy_port);
			}
		} catch (Exception e) {
			args.showHelp();
		}
	}

	/**
	 * 设置邮件发送器信息
	 */
	private static void setMailSenderInfo(CkMailMan mm) {
		mm.put_SmtpHost("smtp." + DEFAULT_MAIL_DOMAIN);
		mm.put_SmtpUsername(DEFAULT_MAIL_USERNAME);
		mm.put_SmtpPassword(DEFAULT_MAIL_PASSWORD);
	}

	/**
	 * 生成邮件
	 */
	private static CkEmail getMail(InputArgs args) {
		CkEmail mail = new CkEmail();
		mail.put_From(DEFAULT_MAIL_USERNAME + "@" + DEFAULT_MAIL_DOMAIN);

		String mailTo = args.get("-t");
		if (mailTo == null) {
			args.showHelp();
		}
		mail.AddTo(mailTo, mailTo);

		String subject = args.get("-s");
		String body = args.get("-b");
		String attach = args.get("-a");
		if (subject != null) {
			mail.put_Subject(subject);
		} else if (attach != null) {
			mail.put_Subject(attach);
		} else {
			args.showHelp();
		}
		if (body != null) {
			mail.put_Body(body);
		}
		if (attach != null) {
			mail.addFileAttachment(attach);
		}
		return mail;
	}

	/**
	 * 发送邮件
	 */
	private static void sendMail(CkMailMan mm, CkEmail mail) {
		try {
			System.out.print("mail sending...");
			if (mm.SendEmail(mail)) {
				System.out.println("success!");
			} else {
				System.out.println("failure!");
			}
		} finally {
			mm.CloseSmtpConnection();
		}
	}
}
